home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 118 / cd-rom 118.iso / aplic / open / openofficeorg1.cab / MemoryUsage.java < prev    next >
Encoding:
Java Source  |  2005-02-14  |  4.8 KB  |  143 lines

  1. import java.util.Random;
  2. import java.util.Date;
  3. import com.sun.star.uno.UnoRuntime;
  4. import com.sun.star.uno.AnyConverter;
  5. import com.sun.star.uno.Type;
  6. import com.sun.star.uno.XInterface;
  7. import com.sun.star.lang.XComponent;
  8. import com.sun.star.lang.XMultiServiceFactory;
  9. import com.sun.star.frame.XComponentLoader;
  10. import com.sun.star.document.XEmbeddedObjectSupplier;
  11. import com.sun.star.awt.ActionEvent;
  12. import com.sun.star.awt.Rectangle;
  13. import com.sun.star.beans.XPropertySet;
  14. import com.sun.star.beans.PropertyValue;
  15.  
  16. import com.sun.star.container.*;
  17. import com.sun.star.chart.*;
  18. import com.sun.star.table.*;
  19. import com.sun.star.sheet.*;
  20.  
  21. import com.sun.star.script.provider.XScriptContext;
  22.  
  23. public class MemoryUsage
  24. {
  25.     // public void updateMemoryUsage(XScriptContext ctxt, ActionEvent evt)
  26.     public void updateMemoryUsage(XScriptContext ctxt)
  27.         throws Exception
  28.     {
  29.         XSpreadsheet sheet = createSpreadsheet(ctxt);
  30.  
  31.         Runtime runtime = Runtime.getRuntime();
  32.         Random generator = new Random();
  33.         Date date = new Date();
  34.  
  35.         // allocate a random amount of memory
  36.         int len = (int)(generator.nextFloat() * runtime.freeMemory() / 5);
  37.         byte[] bytes = new byte[len];
  38.  
  39.         addData(sheet, date.toString(),
  40.             runtime.totalMemory(), runtime.freeMemory());
  41.  
  42.         addChart(sheet);
  43.     }
  44.  
  45.     private XSpreadsheet createSpreadsheet(XScriptContext ctxt)
  46.         throws Exception
  47.     {
  48.         XComponentLoader loader = (XComponentLoader)
  49.             UnoRuntime.queryInterface(
  50.                 XComponentLoader.class, ctxt.getDesktop());
  51.  
  52.         XComponent comp = loader.loadComponentFromURL(
  53.             "private:factory/scalc", "_blank", 4, new PropertyValue[0]);
  54.  
  55.         XSpreadsheetDocument doc = (XSpreadsheetDocument)
  56.             UnoRuntime.queryInterface(XSpreadsheetDocument.class, comp);
  57.  
  58.         XIndexAccess index = (XIndexAccess)
  59.             UnoRuntime.queryInterface(XIndexAccess.class, doc.getSheets());
  60.  
  61.         XSpreadsheet sheet = (XSpreadsheet) AnyConverter.toObject(
  62.             new Type(com.sun.star.sheet.XSpreadsheet.class), index.getByIndex(0));
  63.  
  64.         return sheet;
  65.     }
  66.  
  67.     private void addData(
  68.         XSpreadsheet sheet, String date, long total, long free)
  69.         throws Exception
  70.     {
  71.         sheet.getCellByPosition(0, 0).setFormula("Used");
  72.         sheet.getCellByPosition(0, 1).setFormula("Free");
  73.         sheet.getCellByPosition(0, 2).setFormula("Total");
  74.  
  75.         sheet.getCellByPosition(1, 0).setValue(total - free);
  76.         sheet.getCellByPosition(1, 1).setValue(free);
  77.         sheet.getCellByPosition(1, 2).setValue(total);
  78.     }
  79.  
  80.     private void addChart(XSpreadsheet sheet)
  81.         throws Exception
  82.     {
  83.         Rectangle rect = new Rectangle();
  84.         rect.X = 500;
  85.         rect.Y = 3000;
  86.         rect.Width = 10000;
  87.         rect.Height = 8000;
  88.  
  89.         XCellRange range = (XCellRange)
  90.             UnoRuntime.queryInterface(XCellRange.class, sheet);
  91.  
  92.         XCellRange myRange =
  93.             range.getCellRangeByName("A1:B2");
  94.  
  95.         XCellRangeAddressable rangeAddr = (XCellRangeAddressable)
  96.             UnoRuntime.queryInterface(XCellRangeAddressable.class, myRange);
  97.  
  98.         CellRangeAddress myAddr = rangeAddr.getRangeAddress();
  99.  
  100.         CellRangeAddress[] addr = new CellRangeAddress[1];
  101.         addr[0] = myAddr;
  102.  
  103.         XTableChartsSupplier supp = (XTableChartsSupplier)
  104.             UnoRuntime.queryInterface( XTableChartsSupplier.class, sheet);
  105.  
  106.         XTableCharts charts = supp.getCharts();
  107.         charts.addNewByName("Example", rect, addr, false, true);
  108.  
  109.         try { Thread.sleep(3000); } catch (java.lang.InterruptedException e) { }
  110.  
  111.         // get the diagram and Change some of the properties
  112.         XNameAccess chartsAccess = (XNameAccess)
  113.             UnoRuntime.queryInterface( XNameAccess.class, charts);
  114.  
  115.         XTableChart tchart = (XTableChart)
  116.             UnoRuntime.queryInterface(
  117.                 XTableChart.class, chartsAccess.getByName("Example"));
  118.  
  119.         XEmbeddedObjectSupplier eos = (XEmbeddedObjectSupplier)
  120.             UnoRuntime.queryInterface( XEmbeddedObjectSupplier.class, tchart );
  121.  
  122.         XInterface xifc = eos.getEmbeddedObject();
  123.  
  124.         XChartDocument xChart = (XChartDocument)
  125.             UnoRuntime.queryInterface(XChartDocument.class, xifc);
  126.  
  127.         XMultiServiceFactory xDocMSF = (XMultiServiceFactory)
  128.             UnoRuntime.queryInterface(XMultiServiceFactory.class, xChart);
  129.  
  130.         Object diagObject =
  131.             xDocMSF.createInstance("com.sun.star.chart.PieDiagram");
  132.  
  133.         XDiagram xDiagram = (XDiagram)
  134.             UnoRuntime.queryInterface(XDiagram.class, diagObject);
  135.  
  136.         xChart.setDiagram(xDiagram);
  137.  
  138.         XPropertySet propset = (XPropertySet)
  139.             UnoRuntime.queryInterface( XPropertySet.class, xChart.getTitle() );
  140.         propset.setPropertyValue("String", "JVM Memory Usage");
  141.     }
  142. }
  143.